#!/bin/bash

# cjgit - Status Command
# Show status of all Cangjie projects

# Source common functions
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/cjgit-common.sh"

# Status command implementation
status_command() {
    print_info "Checking status of all Cangjie projects..."
    echo
    
    # Initialize badge tracking variables
    local pull_shown=false
    local push_shown=false
    local changes_shown=false
    local myfork_shown=false
    
    for project in "${PROJECTS[@]}"; do
        # Check if project directory exists and is a git repository
        if [[ ! -d "$project" ]] || [[ ! -d "$project/.git" ]]; then
            echo -e "${BOLD}[$project]${NC} --not-found--"
            continue
        fi
        
        # Get current branch
        current_branch=$(get_current_branch "$project")
        if [[ -z "$current_branch" ]]; then
            current_branch="--detached--"
        fi
        
        # Initialize badges
        pull_badge=""
        push_badge=""
        changes_badge=""
        myfork_badge=""
        
        # Check for [pull] badge (upstream changes)
        upstream_remote=$(find_cangjie_remote "$project")
        if [[ -n "$upstream_remote" ]] && upstream_has_changes "$project" "$upstream_remote"; then
            pull_badge="${YELLOW}[pull]${NC}"
        fi
        
        # Check for [push] badge (changes to push to myfork)
        if has_changes_to_push "$project"; then
            push_badge="${GREEN}[push]${NC}"
        fi
        
        # Check for [myfork] badge (conflicts with myfork - behind remote)
        if has_conflicts_with_myfork "$project"; then
            myfork_badge="${RED}[myfork]${NC}"
        fi
        
        # Check for [changes] badge (local uncommitted changes)
        if has_local_changes "$project"; then
            changes_badge="${RED}[local changes]${NC}"
        fi
        
        # Build the output line with proper color formatting
        line="${BOLD}[$project]${NC} $current_branch"
        
        # Add badges with proper spacing
        if [[ -n "$pull_badge" ]]; then
            line="$line $pull_badge"
        fi
        
        if [[ -n "$push_badge" ]]; then
            line="$line $push_badge"
        fi
        
        if [[ -n "$myfork_badge" ]]; then
            line="$line $myfork_badge"
        fi
        
        if [[ -n "$changes_badge" ]]; then
            line="$line $changes_badge"
        fi
        
        # Print the line with echo -e to interpret escape sequences
        echo -e "$line"
        
        # Track which badges were shown for this project
        if [[ -n "$pull_badge" ]]; then
            pull_shown=true
        fi
        if [[ -n "$push_badge" ]]; then
            push_shown=true
        fi
        if [[ -n "$myfork_badge" ]]; then
            myfork_shown=true
        fi
        if [[ -n "$changes_badge" ]]; then
            changes_shown=true
        fi
    done
    
    # Print legend for badges that were actually shown
    echo
    if [[ "$pull_shown" == true ]] || [[ "$push_shown" == true ]] || [[ "$myfork_shown" == true ]] || [[ "$changes_shown" == true ]]; then
        echo "Legend:"
        if [[ "$pull_shown" == true ]]; then
            echo -e "  ${YELLOW}[pull]${NC}    - Upstream remote has changes that can be pulled"
        fi
        if [[ "$push_shown" == true ]]; then
            echo -e "  ${GREEN}[push]${NC}    - Local commits can be pushed to fork repository"
        fi
        if [[ "$myfork_shown" == true ]]; then
            echo -e "  ${RED}[myfork]${NC}   - Fork repository has conflicting changes (behind remote)"
        fi
        if [[ "$changes_shown" == true ]]; then
            echo -e "  ${RED}[local changes]${NC} - Uncommitted changes to tracked files"
        fi
    fi
}

# Help function for status command
help_command() {
    echo "Usage: cjgit status"
    echo
    echo "Show status of all Cangjie projects"
    echo
    echo "This command displays the current branch and status badges for each project."
    echo
    echo "Examples:"
    echo "  cjgit status                   # Show status of all projects"
}

# Main execution
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
    help_command
    exit 0
fi

status_command